package com.iplant.wms.serviceImpl.dao.brm;


import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.cache.CacheUtils;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.mss.MSSMaterial;
import com.iplant.wms.service.po.brm.BRMShelvesRange;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
@DBBaseDAO(BRMShelvesRange.class)
public class BRMShelvesRangeDAO extends BaseDAO {

    private static BRMShelvesRangeDAO Instance = null;


    private BRMShelvesRangeDAO() {
        super("");
        this.Init();
    }

    public static BRMShelvesRangeDAO getInstance() {
        if (Instance == null) {
            Instance = new BRMShelvesRangeDAO();
        }
        return Instance;
    }


    public void BRM_DeleteShelvesRange(BMSEmployee wLoginUser, int wRuleID, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);

        try {
            if (wRuleID <= 0) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }
            Map<String, Object> wDeleteParams = new HashMap<>();
            wDeleteParams.put("RuleID", wRuleID);
            this.Delete(wDeleteParams, " and ID>0 ");

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error("BRM_DeleteShelvesRange", ex);
        }
    }


    public BRMShelvesRange BRM_QueryShelvesRange(BMSEmployee wLoginUser, int wStoreID, int wCustomerID,
                                                 String wMaterialNo, OutResult<Integer> wErrorCode) {
        BRMShelvesRange wResult = new BRMShelvesRange();
        try {


            MSSMaterial mssMaterial = CacheUtils.GetCacheInfo(MSSMaterial.class, -1, wMaterialNo);

            if (mssMaterial == null || mssMaterial.ID <= 0) {
                return wResult;
            }
            wResult = this.BRM_QueryShelvesRange(wLoginUser, wStoreID, wCustomerID, mssMaterial.MaterialGroupName, mssMaterial.ID,
                    wErrorCode);

            if (wErrorCode.Result != 0) {
                return wResult;
            }
            if (wResult.ID <= 0) {
                wResult = this.BRM_QueryShelvesRange(wLoginUser, wStoreID, wCustomerID, "", 0,
                        wErrorCode);
            }
            if (wErrorCode.Result != 0) {
                return wResult;
            }
            if (wResult.ID <= 0) {
                wResult = this.BRM_QueryShelvesRange(wLoginUser, wStoreID, 0, "", 0,
                        wErrorCode);
            }

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error("BRM_QueryShelvesRange", ex);
        }
        return wResult;
    }


    public BRMShelvesRange BRM_QueryShelvesRange(BMSEmployee wLoginUser, int wStoreID, int wCustomerID,
                                                 String wMaterialGroupFullName, int wMaterialID,
                                                 OutResult<Integer> wErrorCode) {
        BRMShelvesRange wResult = new BRMShelvesRange();
        try {

            List<BRMShelvesRange> wList =
                    this.BRM_QueryShelvesRangeList(wLoginUser, wStoreID, StringUtils.parseListArgs(wCustomerID),
                            -1, wMaterialGroupFullName,
                            wMaterialID, "", "", Pagination.getMaxSize(), wErrorCode);

            if (wErrorCode.Result != 0 || wList == null || wList.size() == 0) {
                return wResult;
            }

            for (BRMShelvesRange mssShelvesRange : wList) {

                if (mssShelvesRange.MaterialID == wMaterialID) {
                    return mssShelvesRange;
                }
            }

            wList.sort((o1, o2) -> o2.MaterialGroupName.length() - o1.MaterialGroupName.length());

            wResult = wList.get(0);
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error("BRM_QueryShelvesRange", ex);
        }
        return wResult;
    }


    public List<BRMShelvesRange> BRM_QueryShelvesRangeList(
            BMSEmployee wLoginUser, int wStoreID, List<Integer> wCustomerIDList, int wMaterialGroupID,
            String wMaterialGroupFullName,
            int wMaterialID, String wMaterialCode, String wMaterialLike,
            Pagination wPagination, OutResult<Integer> wErrorCode) {


        if (wCustomerIDList == null) {
            wCustomerIDList = new ArrayList<>();
        }
        wCustomerIDList.removeIf(p -> p <= 0);
        // Step02:查询
        Map<String, Object> wParams = new HashMap<String, Object>();


        wParams.put("CustomerIDList", wCustomerIDList);
        wParams.put("StoreID", wStoreID);
        wParams.put("MaterialGroupID", wMaterialGroupID);
        wParams.put("MaterialFullGroup", wMaterialGroupFullName);
        wParams.put("MaterialID", wMaterialID);
        wParams.put("MaterialName", wMaterialLike);
        wParams.put("MaterialCodeEQ", wMaterialCode);

        return super.SelectPOList(wLoginUser, wParams, wPagination, wErrorCode);

    }


}
